package com.peanut.service.impl;

import com.peanut.domain.bo.ForumCommentBO;
import com.peanut.domain.po.ForumComment;
import com.peanut.domain.po.ForumTitle;
import com.peanut.domain.po.TabAdmin;
import com.peanut.service.ForumCommentService;
import com.peanut.service.ForumTitleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ForumCommentServiceImpl implements ForumCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ForumTitleService forumTitleService;

    @Override
    public boolean insertComment(ForumCommentBO bo, TabAdmin tabAdmin) {
        //通过帖子id找到这一条帖子
        ForumTitle title = forumTitleService.selectTitleById(bo.getForumId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        bo.setDate(sdf.format(date));
        //设置帖子名
        bo.setForumTitle(title.getForumTitle());
        bo.setLike(0L);
        ForumComment forumComment = new ForumComment();
        //BO复制到PO实体类
        BeanUtils.copyProperties(bo,forumComment);
        mongoTemplate.insert(forumComment);
        //评论成功后，在该帖子上的评论数+1
        forumTitleService.updateCommentNum(title.getId());
        return true;
    }

    @Override
    public boolean insertChildComment(ForumCommentBO bo, TabAdmin tabAdmin) {
        //通过上级评论id找到这一条主评论
        ForumComment comment = mongoTemplate.findById(bo.getFatherCommentId(), ForumComment.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        bo.setDate(sdf.format(date));
        //设置帖子名
        bo.setForumTitle(comment.getForumTitle());
        //设置上级评论
        bo.setFatherComment(comment.getForumComment());
        //设置帖子id
        bo.setForumId(comment.getForumId());
        //设置上级评论id
        bo.setFatherCommentId(comment.getId());
        bo.setLike(0L);
        ForumComment forumComment = new ForumComment();
        //BO复制到PO实体类
        BeanUtils.copyProperties(bo,forumComment);
        mongoTemplate.insert(forumComment);
        //评论成功后，在该帖子上的评论数+1
        forumTitleService.updateCommentNum(comment.getForumId());
        return true;
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query(Criteria.where("forum_id").is(id));
        mongoTemplate.remove(query,ForumComment.class);
        return true;
    }

    @Override
    public Long deleteComment(String id) {
        //删除后返回删除条数
        long num1 = mongoTemplate.remove(
                new Query(Criteria.where("_id").is(id)),
                ForumComment.class).getDeletedCount();
        long num2 = mongoTemplate.remove(
                new Query(Criteria.where("father_comment_id").is(id)),
                ForumComment.class).getDeletedCount();
        long l = -(num1 + num2);
        return l;
    }

    @Override
    public ForumComment selectById(String id) {
        return mongoTemplate.findById(id,ForumComment.class);
    }

    @Override
    public void likeComment(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("like",1L);
        mongoTemplate.updateFirst(query,update,ForumComment.class);
    }

    @Override
    public void updateComment(String id, String forumComment) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("forum_comment",forumComment);
        mongoTemplate.updateFirst(query,update,ForumComment.class);
    }

    @Override
    public List<Map<String, Object>> getComments(String id) {
        //遍历帖子id查询所有评论+子评论
        List<ForumComment> comments = mongoTemplate.find(
                new Query(Criteria.where("forum_id").is(id)), ForumComment.class);

        List<Map<String, Object>> voList = getChildComments(comments, "628fa9d00b4c1f258f359b4f");

        return voList;
    }

    private List<Map<String, Object>> getChildComments(List<ForumComment> comments, String id) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        //遍历comments
        for (int i = 0; i < comments.size(); i++){
            Map<String, Object> map = null;
            ForumComment comment = comments.get(i);
            //如果这个评论的父级评论为空,说明是主评论
            if (comment.getFatherCommentId() == null){
                map = new HashMap<String, Object>();
                map.put("id", comment.getId());
                map.put("forumTitle", comment.getForumTitle());
                map.put("date", comment.getDate());
                map.put("forumComment", comment.getForumComment());
                map.put("user", comment.getUser());
                map.put("like", comment.getLike());
                continue;
            }
            //如果父级id为传入的评论id
            if (comment.getFatherCommentId().equals(id)) {
                map = new HashMap<String, Object>();
                map.put("id", comment.getId());
                map.put("forumTitle", comment.getForumTitle());
                map.put("date", comment.getDate());
                map.put("forumComment", comment.getForumComment());
                map.put("user", comment.getUser());
                map.put("like", comment.getLike());
                map.put("fatherComment", comment.getFatherComment());

                map.put("fatherCommentId", comment.getFatherCommentId());
                map.put("commentVOList", getChildComments(comments, comment.getId()));
            }
            if (map != null) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("commentVOList");
                if (list.size() <= 0) {
                    map.remove("commentVOList");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

}
